#!/bin/bash

# Slackware build script for TeighaFileConverter

# Copyright 2012-2014  Fridrich von Stauffenberg <email removed>
# Copyright 2018 B. Watson <yalhcru@gmail.com>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20180724 bkw:
# - Take over maintenance.
# - Update for v19.5.0.0. This adds x86_64 support, but means qt5 is
#   now required.
# - Pacify desktop-file-validate.
# - Simplify script a bit.
# - Clarify README and slack-desc.
# - Name the x86 package according to ARCH (i586 or i686). No effect on
#   the package contents, just the filename.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=teighafileconverter
VERSION=${VERSION:-19.5.0.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

ORIGNAM=TeighaFileConverter

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

case "$ARCH" in
  i?86)   DEBARCH=X86 ; LIBDIRSUFFIX=""   ;;
  x86_64) DEBARCH=X64 ; LIBDIRSUFFIX="64" ;;
  *)      echo "Unsupported ARCH, only i?86 and x86_64 supported" 1>&2
          exit 1 ;;
esac

set -e

# No "source" directory, extract directly to $PKG.

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $PKG

# How to extract a .deb without creating temp files. If you're using this
# in your own SlackBuild, be aware that some .debs contain data.tar.xz
# instead of .gz.
ar p $CWD/${ORIGNAM}_QT5_lnx${DEBARCH}_*dll.deb data.tar.gz | tar xvfz -

chown -R root:root .
find . -type d -exec chmod 755 {} \;
find -L . \! -name '*.so' \! -name '*.tx*' -type f -exec chmod 644 {} \;

# Binaries aren't stripped, according to 'file', but they don't get any
# smaller if I strip them, so leave them as-is.

PKGLIB=$PKG/usr/lib$LIBDIRSUFFIX
mkdir -p $PKGLIB
mv $PKG/usr/bin/${ORIGNAM}_$VERSION $PKGLIB/$ORIGNAM
sed "s,@LIBDIRSUFFIX@,$LIBDIRSUFFIX,g" $CWD/$PRGNAM.sh > $PKG/usr/bin/$ORIGNAM
chmod 755 $PKG/usr/bin/$ORIGNAM $PKGLIB/$ORIGNAM/$ORIGNAM

# .desktop file spec says they *must* be UTF-8, upstream used 8859-1...
PKGAPP=$PKG/usr/share/applications
iconv -f ISO-8859-1 -t UTF-8 $PKGAPP/${ORIGNAM}_$VERSION.desktop | \
  sed 's@/usr/bin/@@g' > \
  $PKGAPP/${ORIGNAM}.desktop
rm -f $PKGAPP/${ORIGNAM}_$VERSION.desktop

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
mv $PKG/usr/share/doc/$ORIGNAM/* $PKGDOC
rm -rf $PKG/usr/share/doc
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
